using System;
using UnityEngine;

[Serializable]
public class TTCameraBehaviourFlyAroundObject : TTCameraBehaviour
{
	public Vector3 ellipseCentre;

	public float zSemiAxis;

	public float xSemiAxis;

	public float moveIntoPositionSpeed;

	public float baseCircleRotationSpeed;

	public Vector3 initialPos;

	public Vector3 initialRot;

	private float currentAngleWrtZ;

	public float numericTol = 0.01f;

	public float sweepAngleBeforeDone;

	private bool hasStartedFly;

	private float angleForFullRevolution;

	public override void Start(Camera cam, TTCameraController camController, TTCameraOptions cameraOptions)
	{
		base.Start(cam, camController, cameraOptions);
		Vector3 vector = (initialPos - ellipseCentre).OnGround();
		currentAngleWrtZ = Mathf.Acos(vector.z / vector.magnitude);
		angleForFullRevolution = sweepAngleBeforeDone + currentAngleWrtZ;
		hasStartedFly = false;
	}

	public override void Update()
	{
		bool flag = (camera.transform.position - initialPos).magnitude < numericTol && (camera.transform.rotation.eulerAngles - initialRot).magnitude < numericTol;
		if (hasStartedFly || flag)
		{
			FlyAroundCourt();
			if (currentAngleWrtZ >= angleForFullRevolution)
			{
				cameraController.BehaviourDone();
			}
			hasStartedFly = true;
		}
		else
		{
			camera.transform.position = Vector3.Lerp(camera.transform.position, initialPos, RealTime.deltaTime * moveIntoPositionSpeed);
			camera.transform.rotation = Quaternion.Lerp(camera.transform.rotation, Quaternion.Euler(initialRot), RealTime.deltaTime * baseCircleRotationSpeed);
		}
	}

	private void FlyAroundCourt()
	{
		float num = baseCircleRotationSpeed * RealTime.deltaTime;
		currentAngleWrtZ += num * (float)Math.PI / 180f;
		camera.transform.position = new Vector3(xSemiAxis * Mathf.Sin(currentAngleWrtZ), camera.transform.position.y, zSemiAxis * Mathf.Cos(currentAngleWrtZ));
		camera.transform.Rotate(Vector3.up, num, Space.World);
	}
}
